package com.puboot.module.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.common.util.Constants;
import com.puboot.common.util.CoreConst;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.model.*;
import com.puboot.module.admin.service.BizRotationService;
import com.puboot.module.admin.vo.base.PageResultVo;
import com.puboot.module.admin.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.ListUtils;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("rotationimg")
@AllArgsConstructor
public class BizRotationImgController {

    private final BizRotationService bizRotationService;

    @PostMapping("list")
    @ResponseBody
    public PageResultVo loadBizRotationImgList(BizRotationImg bizRotationImg, Integer pageNumber, Integer pageSize) {
        IPage<BizRotationImg> bizRotationImgIPage = bizRotationService.bizRotationImgAll(bizRotationImg, pageNumber, pageSize);
        return ResultUtil.table(bizRotationImgIPage.getRecords(), bizRotationImgIPage.getTotal());
    }

    @GetMapping("/add")
    public String add(Model model, Integer id) {
        BizRotationImg bizRotationImg = bizRotationService.selectById(id);
        model.addAttribute("bizRotationImg", bizRotationImg);
        return CoreConst.ADMIN_PREFIX + "rotationimg/form";
    }

    @PostMapping("/add")
    @ResponseBody
    @CacheEvict(value = "bizRotationImg", allEntries = true)
    public ResponseVo add(BizRotationImg bizRotationImg) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        bizRotationImg.setCreateTime(new Date());
        bizRotationImg.setStatus(CoreConst.STATUS_VALID);
        bizRotationImg.setCreateUserId(user.getUserId());
        boolean i = bizRotationService.save(bizRotationImg);
        if (i) {
            return ResultUtil.success("新增轮播图成功");
        } else {
            return ResultUtil.error("新增轮播图失败");
        }
    }

    @PostMapping("/edit")
    @ResponseBody
    @CacheEvict(value = "category", allEntries = true)
    public ResponseVo edit(BizRotationImg bizRotationImg) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        bizRotationImg.setUpdateTime(new Date());
        bizRotationImg.setUpdateUserId(user.getUserId());
        boolean i = bizRotationService.updateById(bizRotationImg);
        if (i) {
            return ResultUtil.success("编辑轮播图成功");
        } else {
            return ResultUtil.error("编辑轮播图失败");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public ResponseVo update(Integer id, Integer status) {
        String msg = status == 0 ? "禁用" : "启用";
        BizRotationImg bizRotationImg = bizRotationService.selectById(id);
        bizRotationImg.setUpdateTime(new Date());
        bizRotationImg.setStatus(status);
        boolean i = bizRotationService.updateById(bizRotationImg);
        if (i) {
            return ResultUtil.success(msg + "轮播图成功");
        } else {
            return ResultUtil.error(msg + "轮播图失败");
        }
    }
}
